package com.huike.web.controller.review;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult add1(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
        Integer count = reviewService.saveData1(name,age,sex);
        return new AjaxResult(200,"成功插入:"+count+"条数据");
    }
    @PostMapping("/saveData")
    public AjaxResult add2(@RequestBody Review review){
        Integer count = reviewService.saveData2(review);
        return new AjaxResult(200,"成功插入:"+count+"条数据");
    }
    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){
        Integer count = reviewService.delete(id);
        if (count == 1) {
            return new AjaxResult(200,"删除成功");
        }
        return new AjaxResult(201,"删除失败");
    }
    /**=========================================================修改数据=============================================*/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        Integer count = reviewService.update(review);
        if (count == 1) {
            return new AjaxResult(200,"修改成功");
        }
        return new AjaxResult(201,"修改失败");
    }
    /**=========================================================查询数据=============================================*/
    @GetMapping("/getById")
    public AjaxResult getById(@RequestParam Long id){
        Review review = reviewService.getById(id);
        return new AjaxResult(200,"操作成功",review);
    }

    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Review> reviewList = reviewService.selectAll();
        return getDataTable(reviewList);
    }
}